<script setup lang="ts">
import { useMemberStore, useDeviceStore } from '@/stores'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
const memberStore = useMemberStore()
const deviceStore = useDeviceStore()


//跳转到实时数据页面
const getData = (id: number,deviceNum:string) => {
  uni.navigateTo({
    url: `/pages/index/compents/real-time-data/real-time-data?id=${id}&deviceNum=${deviceNum}`,
  })
}

const draw = () => {
  uni.switchTab({
    url: '/pages/my/my',
  })
}

const getInfo = () => {
  if (memberStore.profile) {
    deviceStore.getDeviceList()
  }
}
onLoad(() => {
  getInfo()
})

const isRefresh = ref(false)
const onRefresh = async () => {
  //console.log('刷新')
  isRefresh.value = true
  await deviceStore.getDeviceList()
  isRefresh.value = false
}
</script>

<template>
  <view class="content" v-if="deviceStore.deviceList">
    <view class="title"> 首页 </view>
    <scroll-view
      scroll-y
      class="scroll-view"
      :refresher-enabled="true"
      @refresherrefresh="onRefresh"
      :refresher-triggered="isRefresh"
    >
      <view
        class="info_card"
        v-for="item in deviceStore.deviceList"
        :key="item.id"
        @tap="getData(item.id,item.deviceNum)"
      >
        <view class="prile_box">
          <up-avatar :src="item.picture" size="65"></up-avatar>
          <view class="prile">
            <text v-if="item.name">{{ item.name }}</text>
            <text v-else> {{ '暂未设置设备昵称' }}</text>
            <text v-if="item.deviceNum">{{ '设备ID:' + item.deviceNum }}</text>
          </view>
        </view>
        <u-grid :border="false">
          <u-grid-item>
            <view class="body_data frist">{{ item.isInBed ? '在床' : '离床' }}</view>
            <view class="status">状态</view>
          </u-grid-item>
          <u-grid-item>
            <view class="body_data">{{ item.heart ? item.heart : '--' }}</view>
            <view class="parameter">次/分</view>
            <view class="status">心率</view>
          </u-grid-item>
          <u-grid-item>
            <view class="body_data">{{ item.breath ? item.breath : '--' }}</view>
            <view class="parameter">次/分</view>
            <view class="status">呼吸</view>
          </u-grid-item>
        </u-grid>
      </view>
      <!-- scroll-view高度为100%，会遮挡底部盒子阴影，放一个空盒子占位 -->
      <view class="placeholder"></view>
    </scroll-view>
  </view>
  <view class="content" v-if="!deviceStore.deviceList || deviceStore.deviceList?.length === 0">
    <view class="title"> 首页 </view>
    <view class="nodevice">
      <u-button type="primary" shape="circle" color="#3780A6" @click="draw">前往绑定</u-button>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100vh;  
  font-family: PingFangSC-regular;
  overflow: hidden;
  .content {
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: url(@/static/starry2.png) no-repeat;
    background-size: contain;
    .title {
      padding-top: 115.385rpx;
      padding-bottom: 28.846rpx;
      font-size: 34.615rpx;
      color: #ffff;
      text-align: center;
    }
    .scroll-view {
      height: calc(100vh - 115.385rpx - 34.615rpx - 28.846rpx);
      overflow: hidden;
      padding-top: 28.846rpx;
       .info_card {
        /* scroll-view不支持flex布局，这里使用auto来实现水平居中 */
        margin: 0 auto 38.462rpx;
        width: 651.923rpx;
        height: 317.308rpx;
        border-radius: 19.231rpx;
        background-color: rgba(255, 255, 255, 1);
        box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
        border: 1px solid rgba(187, 187, 187, 1);
        .prile_box {
          padding-top: 26.923rpx;
          padding-left: 23.077rpx;
          width: 576.923rpx;
          display: flex;
          font-size: 26.923rpx;
          .prile {
            display: flex;
            height: 125rpx;
            padding-left: 38.462rpx;
            flex-direction: column;
            justify-content: space-between;
            font-family: PingFangSC-light;
            text {
              &:first-child {
                font-size: 46.154rpx;
              }
            }
          }
        }

        .body_data {
          margin-top: 19.231rpx;
          height: 48.077rpx;
          font-size: 38.462rpx;
          line-height: 48.077rpx;
        }
        .frist {
          font-size: 38.462rpx;
          margin-bottom: 23.077px;
        }
        .status {
          font-size: 30.769rpx;
          color: #9a9a9a;
        }
        .parameter {
          font-size: 26.923rpx;
          color: #9a9a9a;
          font-family: PingFangSC-light;
        }
      }
      .placeholder {
        height: 19.231rpx;
      }
    }
    .nodevice {
      margin: 0 auto;
      width: 634.615rpx;
      height: 76.923rpx;
      margin-top: 196.154rpx;
    }
  }
}
</style>
